package com.nf.practice.downloadFile;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void main(String[] args) {
        // 创建JSON对象
        JSONObject json = new JSONObject();
        json.put("02202312271339440303", "http://172.16.10.2:30100/fs-bucket/2023/12/27/ab9728c6394d41cc9d489cac58e98e3d.jpg");
        json.put("02202312270951390012", "http://172.16.10.2:30100/fs-bucket/2023/12/27/e0d7c90ee1004b308112318b8b05c379.jpg");

        // 创建文件夹并下载文件
        String folderPath = "folder"; // 文件夹路径
        File folder = new File(folderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        for (String key : json.keySet()) {
            String value = json.getString(key);
            String filePath = folderPath + "/" + key + ".pdf";
            downloadFile(value, filePath);        }

        // 打包文件夹为压缩包
        String zipFilePath = "folder.zip"; // 压缩包路径
        zipFolder(folderPath, zipFilePath);

        // 删除文件夹
        deleteFolder(folder);

        System.out.println("文件夹打包完成，压缩包已下载。");
    }

    // 下载文件
    private static void downloadFile(String fileUrl, String filePath) {
        try {
            URL url = new URL(fileUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            InputStream inputStream = connection.getInputStream();
            FileOutputStream outputStream = new FileOutputStream(filePath);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }

            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 打包文件夹为压缩包
    private static void zipFolder(String folderPath, String zipFilePath) {
        try {
            FileOutputStream fos = new FileOutputStream(zipFilePath);
            ZipOutputStream zos = new ZipOutputStream(fos);

            File folder = new File(folderPath);
            zipFolder(folder, folder.getName(), zos);

            zos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void zipFolder(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                zipFolder(file, parentFolder + "/" + file.getName(), zos);
                continue;
            }

            FileInputStream fis = new FileInputStream(file);
            ZipEntry zipEntry = new ZipEntry(parentFolder + "/" + file.getName());
            zos.putNextEntry(zipEntry);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }

            fis.close();
        }
    }

    // 删除文件夹
    private static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            for (File file : folder.listFiles()) {
                deleteFolder(file);
            }
        }
        folder.delete();
    }
}